package com.xuxin.nantai.activity.chou;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.xuxin.nantai.R;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.bean.SelectDefectBean;
import com.xuxin.nantai.contract.SelectDefectContract;
import com.xuxin.nantai.presenter.SelectDefectPresenter;
import com.xuxin.nantai.view.myNumber;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.09 0009
 * 缝制抽检提取瑕疵页(0.5单位)
 */
public class DefectActivity extends BaseActivity implements SelectDefectContract.View {
    private SelectDefectContract.Presenter presenter = new SelectDefectPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private DefectAdapter myAdapter;
    private String Guid;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            Guid = bundle.getString("Guid");
            LogUtils.e("提取瑕疵页_Guid = " + Guid);
        }
        title_name.setText("提取疵点(缝制抽检)");
        title_right.setImageResource(R.mipmap.img_sure);
        title_right.setVisibility(View.VISIBLE);
        smart_refresh.setEnableRefresh(false);
        smart_refresh.setEnableLoadMore(false);
        myAdapter = new DefectAdapter();
        smart_recycle.setLayoutManager(new LinearLayoutManager(this));
        smart_recycle.setItemViewCacheSize(10);
        smart_recycle.setNestedScrollingEnabled(false);
        smart_recycle.setAdapter(myAdapter);
    }

    @Override
    public void initData() {
        myAdapter.setList(null);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
    }

    @Override
    @OnClick({R.id.title_left, R.id.title_right})
    public void onClick(View view) {
        KeyboardUtils.hideSoftInput(this);
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.title_right:
                try {
                    StringBuilder sb = new StringBuilder();
                    double num = 0;
                    for (int i = 0; i < myAdapter.getData().size(); i++) {
                        if (myAdapter.getData().get(i).getDefectNum() > 0) {
                            sb.append(myAdapter.getData().get(i).getDDescribe());
                            sb.append("*");
                            sb.append(myAdapter.getData().get(i).getDefectNum());
                            sb.append(";");
                            num = num + myAdapter.getData().get(i).getDefectNum();
                        }
                    }
                    if (num == 0) {
                        ToastUtils.showShort("未选择瑕疵");
                        sb.append("");
                    }
                    Intent intent = new Intent();
                    intent.putExtra("DefectDes", sb.toString());
                    intent.putExtra("DefectNum", num + "");
                    setResult(BaseConstant.result_code, intent);
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 0:
                    presenter.getDefect(Guid, "缝制抽检");
                    break;
            }
            return false;
        }
    });


    private SelectDefectBean mySelectDefectBean;

    @Override
    public void onSuccess(SelectDefectBean selectDefectBean) {
        this.mySelectDefectBean = selectDefectBean;
        myAdapter.setList(mySelectDefectBean.getData());
    }

    private class DefectAdapter extends BaseQuickAdapter<SelectDefectBean.DataBean, BaseViewHolder> {

        private DefectAdapter() {
            super(R.layout.item_select_defect);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, SelectDefectBean.DataBean item) {
            holder.setText(R.id.item_select_defect_name, item.getDDescribe());
            try {
                holder.setTextColor(R.id.item_select_defect_name, item.getDefectNum() > 0 ?
                        ColorUtils.getColor(R.color.colorRed) : ColorUtils.getColor(R.color.colorText));
            } catch (Exception e) {
                holder.setTextColor(R.id.item_select_defect_name, ColorUtils.getColor(R.color.colorText));
                e.printStackTrace();
            }
            myNumber item_defect_number = holder.getView(R.id.item_select_defect_number);
            LogUtils.e(holder.getAdapterPosition() + " - item.getNumber() = " + item.getDefectNum());
            item_defect_number.setCurrentNum(mySelectDefectBean.getData().get(holder.getAdapterPosition()).getDefectNum());
            item_defect_number.setMaxValue(100);
            item_defect_number.setMinDefaultNum(0);
            item_defect_number.setOnInputNumberListener(new myNumber.OnInputNumberListener() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                    try {
                        if (!StringUtils.isEmpty(charSequence)) {
                            double num = Double.parseDouble(charSequence.toString());
                            num = Math.min(num, 100);
                            mySelectDefectBean.getData().get(holder.getAdapterPosition()).setDefectNum(num);
                            LogUtils.e(holder.getAdapterPosition() + " - num = " + num);
                            LogUtils.e(holder.getAdapterPosition() + " - xxnum = " + item.getDefectNum());
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {
                }
            });
            item_defect_number.setmOnClickInputListener(new myNumber.OnClickInputListener() {
                @Override
                public void onWarningForInventory(double inventory) {
                    ToastUtils.showShort("不得超出限制100");
                }

                @Override
                public void onWarningMinInput(double minValue) {
                    ToastUtils.showShort("不得低于限制0");
                }

                @Override
                public void onWarningMaxInput(double maxValue) {
                    ToastUtils.showShort("不得超出限制100");
                }
            });
        }
    }
}